﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ResUma
{
    public partial class ResUmaSetupFrm : FrmCard
    {
        private Int16 LunaInitiala = 0;
        public ResUmaSetupFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            workConnString = mInfo.workConnectionString;
            lstPK.Add(this.anulCurentTB);
            eSRPDataSet.ResUmaSetup.PropagaModificariColumn.DefaultValue = 0;
            eSRPDataSet.ResUmaSetup.ContracteColumn.DefaultValue = true;
            eSRPDataSet.ResUmaSetup.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            eSRPDataSet.ResUmaSetup.ScanResColumn.DefaultValue = 300;
            eSRPDataSet.ResUmaSetup.OrizExtentColumn.DefaultValue = 2550;
            eSRPDataSet.ResUmaSetup.VertExtentColumn.DefaultValue = 3500;
            eSRPDataSet.ResUmaSetup.ColorFormatColumn.DefaultValue = 0;
            eSRPDataSet.ResUmaSetup.ScanDebugColumn.DefaultValue = false;
            eSRPDataSet.ResUmaSetup.VersiuneCORColumn.DefaultValue = 0;
            eSRPDataSet.ResUmaSetup.SituatieMilitaraColumn.DefaultValue = false;

            //private const int WIA_INTENT_NONE = 0x00000000;
            //private const int WIA_INTENT_IMAGE_TYPE_COLOR = 0x00000001;
            //private const int WIA_INTENT_IMAGE_TYPE_GRAYSCALE =0x00000002;
            //private const int WIA_INTENT_IMAGE_TYPE_TEXT = 0x00000004;
            //private const int WIA_INTENT_IMAGE_TYPE_MASK = 0x0000000F;  

            colorOptionsBindingSource.Add(new ColorOptions("Fara", 0));
            colorOptionsBindingSource.Add(new ColorOptions("Color",1));
            colorOptionsBindingSource.Add(new ColorOptions("Nuante de gri", 2));
            colorOptionsBindingSource.Add(new ColorOptions("Text", 4));
            colorOptionsBindingSource.Add(new ColorOptions("Masca", 16)); 

            numeMacheta = Text;
        }

        private void ResUmaSetupFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString); 

            this.resUmaSetupTableAdapter.Connection = con;
            this.ocupatiiTableAdapter.Connection = con;
            this.ocupatiiTableAdapter.FillByVersiune(this.eSRPDataSet.Ocupatii);
            this.resUmaSetupTableAdapter.FillByCodUnitate(this.eSRPDataSet.ResUmaSetup,ModuleInfo.CodUnitate);
           

            InchideConexiune(this.resUmaSetupTableAdapter.Connection);
            AddButton("Salveaza", global::ERPStudio.Properties.Resources.salvare1, Salvare, ToolStripItemAlignment.Right, true);
            AddButton("Arhiveaza", global::ERPStudio.Properties.Resources._001_41, Arhiveaza, ToolStripItemAlignment.Right, true);
            AddButton("Setari Lunare", global::ERPStudio.Properties.Resources._001_44, Setari_Lunare, ToolStripItemAlignment.Right, true);
            this.listBndSourceMain.Add(resUmaSetupBindingSource);
            listTableAdaptor.Add(this.resUmaSetupTableAdapter.Adapter);
            if (resUmaSetupBindingSource.Count == 0)
            {
                anulCurentTB.ReadOnly = false;
                eSRPDataSet.ResUmaSetup.AnulCurentColumn.DefaultValue = DateTime.Now.Year;
                eSRPDataSet.ResUmaSetup.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
                eSRPDataSet.ResUmaSetup.PrimaryKeyColumn.DefaultValue = 0;
                eSRPDataSet.ResUmaSetup.LunaCurentaColumn.DefaultValue = 1;

                Adauga(this,null);
            }
            LunaInitiala = Convert.ToInt16(((DataRowView)resUmaSetupBindingSource.Current).Row["LunaCurenta"]);
        }

        override public void Salvare(object sender, EventArgs args)
        {
            base.Salvare(sender, args);
            if (EroareSalvare)
                return;
            Int16 LunaInitialaAcum = Convert.ToInt16(((DataRowView)resUmaSetupBindingSource.Current).Row["LunaCurenta"]);
            if (LunaInitialaAcum != LunaInitiala)
            {
                SqlCommand cmd = new SqlCommand("ChangeUnit_ResUma", DeschideConexiune(workConnString));
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("codunitate",ModuleInfo.CodUnitate.ToString()));
                cmd.ExecuteNonQuery();
                LunaInitiala = LunaInitialaAcum;
            }
        }

        private void Setari_Lunare(object sender, EventArgs e)
        {
            SetariLunareFrm frm = new SetariLunareFrm(ModuleInfo,Convert.ToInt16(anulCurentTB.Text));
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void Arhiveaza(object sender, EventArgs e)
        {
            if (MessageBox.Show("Sunteti sigur ca doriti inchiderea lunii curente?",
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            SqlCommand cmd = null;
            MessageBox.Show("Atentie! Se vor inchide toate machetele. Salvati datele, acolo unde este cazul!",
                "Info",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            foreach (StudioForm fChild in MdiParent.MdiChildren)
            {
                if (fChild != this)
                    fChild.Close();
            }
            try
            {
                Cursor.Current = Cursors.AppStarting;
                cmd = new SqlCommand("[resuma_InchideLuna]", DeschideConexiune(workConnString));
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Luna", lunaCurentaNUD.Value.ToString()));
                cmd.Parameters.Add(new SqlParameter("@An", anulCurentTB.Text));
                cmd.Parameters.Add(new SqlParameter("@CodUnitate", ModuleInfo.CodUnitate.ToString()));
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                "Eroare",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
                return;
            }
            finally
            {

                InchideConexiune(cmd.Connection);
                Cursor.Current = Cursors.Default;
            }
            MessageBox.Show("Documentele curente au fost arhivate cu succes!",
                            "Info",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            SetCurentUnit(ModuleInfo.CodUnitate);
        }

        override public void SetCurentMonth(short vCodUnitate)
        {

        }

        override public void SetCurentUnit(short vCodUnitate)
        {
            this.resUmaSetupTableAdapter.Connection = DeschideConexiune(workConnString);
            this.resUmaSetupTableAdapter.FillByCodUnitate(this.eSRPDataSet.ResUmaSetup, vCodUnitate);
            if (resUmaSetupBindingSource.Count == 0)
            {
                resUmaSetupBindingSource.AllowNew = true;
                Adauga(this, null);
                resUmaSetupBindingSource.AllowNew = false;
                anulCurentTB.Enabled = true;
                anulCurentTB.ReadOnly = false;
                lunaCurentaNUD.ReadOnly = false;
                lunaCurentaNUD.Enabled = true;
            }
            Int16 lunaC = Convert.ToInt16(((DataRowView)resUmaSetupBindingSource.Current)["LunaCurenta"]);
            if (lunaC < 12)
                lunaCurentaNUD.Maximum = lunaC + 1;
            InchideConexiune(this.resUmaSetupTableAdapter.Connection);
        }
    }

    [Serializable]
    public class ColorOptions
    {
        public ColorOptions(String nume, Int32 tag)
        {
            _Name = nume;
            _Tag = tag;
        }

        private String _Name = String.Empty;
        [System.ComponentModel.Bindable(true)]
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private Int32 _Tag = 0;
        [System.ComponentModel.Bindable(true)]
        public Int32 Tag
        {
            get
            {
                return _Tag;
            }
            set
            {
                _Tag = value;
            }
        }

    }

}
